
package vinterprojekt.gui;

/**
 *
 * @author jacob
 */

import vinterprojekt.entity.Book;


public class TableHelper
{
    private javax.swing.JTable table;


    public TableHelper(javax.swing.JTable table)
    {
        this.table = table;
    }

    public void updateContent(Book[] books)
    {
        javax.swing.table.DefaultTableModel model = (javax.swing.table.DefaultTableModel) table.getModel();

        // ryd tabellen
        while (model.getRowCount() > 0) model.removeRow(0);

        // og indsæt
        if (books != null)
        {
            for (Book book : books)
            {
                Object[] row = { book.getISBN(), book.getAuthor(), book.getTitle(), book.getPublisher(), book.getEdition(), book.getYearOfPublication() };
                model.addRow(row);
            }
        }

    }

    public String getSelectedIsbn()
    {
        int row = getSelectedRow();
        if (row == -1) return null;
        
        else return table.getValueAt(row, 0).toString();
    }

    public String getSelectedAuthor()
    {
        int row = getSelectedRow();
        return table.getValueAt(row, 1).toString();
    }

    public String getSelectedTitle()
    {
        int row = getSelectedRow();
        return table.getValueAt(row, 2).toString();
    }

    public String getSelectedPublisher()
    {
        int row = getSelectedRow();
        return table.getValueAt(row, 3).toString();
    }

    public String getSelectedEdition()
    {
        int row = getSelectedRow();
        return table.getValueAt(row, 4).toString();
    }

    public String getSelectedYearOfPublication()
    {
        int row = getSelectedRow();
        return table.getValueAt(row, 5).toString();
    }

    private int getSelectedRow()
    {
        //return table.getRowCount() == 1 ? 0 : table.getSelectedRow();
        return table.getSelectedRow();
    }
}
